package com.itheima.health.mobile.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.common.RedisConst;
import com.itheima.health.entity.Result;
import com.itheima.health.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.Map;

/**
 * @autahor: csl
 * @date: 2019/7/26
 * @description: 登录控制器
 * @version:1.0
 */
@RestController
@RequestMapping("/mobile/login")
public class LoginController {
    @Autowired
    private JedisPool jedisPool;
    @Reference
    private LoginService service;
    public Result login(@RequestBody Map<String,String> map) {
        try {
            String validateCode = map.get("validateCode");
            String telephone = map.get("telephone");
            String code = jedisPool.getResource().get(telephone + RedisConst.SENDTYPE_LOGIN);
            if (!code.equals(validateCode)) {
                return new Result(false, MessageConst.VALIDATECODE_ERROR);
            }
            boolean login = service.login(telephone);


            return new Result(true, MessageConst.LOGIN_SUCCESS);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"登录失败");
        }
    }
}
